package com.xh.xyzp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.xyzp.common.constant.UserContext;
import com.xh.xyzp.common.domain.vo.resp.PageResp;
import com.xh.xyzp.common.enums.CommonStatusTypeEnum;
import com.xh.xyzp.domain.entity.Company;
import com.xh.xyzp.domain.entity.Job;
import com.xh.xyzp.domain.vo.JobPageReq;
import com.xh.xyzp.domain.vo.JobResp;
import com.xh.xyzp.domain.vo.JobSubmitReq;
import com.xh.xyzp.enums.ExamineStatusEnum;
import com.xh.xyzp.enums.UserTypeEnum;
import com.xh.xyzp.exception.ApiException;
import com.xh.xyzp.mapper.JobMapper;
import com.xh.xyzp.service.CompanyService;
import com.xh.xyzp.service.JobService;
import com.xh.xyzp.util.AssertUtil;
import com.xh.xyzp.util.SnowFlakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: xianghao
 * @Description:
 * @Date: 2025-04-12  11:12
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private CompanyService companyService;

    @Override
    public void submit(JobSubmitReq req) {
        if (Objects.isNull(req.getExamineStatus())) {
            req.checkParams();
        }
        if (Objects.isNull(req.getId())) {
            Job job = BeanUtil.copyProperties(req, Job.class);
            job.setId(new SnowFlakeIdWorker(1, 0).nextId());
            job.setUserId(UserContext.getUser().getId());
            job.setCompanyId(companyService.getCompanyByUserId(UserContext.getUser().getId()).getId());
            job.setExamineStatus(ExamineStatusEnum.WAIT_EXAMINE.getKey());
            save(job);
        } else {
            Job job = jobMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, job);
            updateById(job);
        }
    }

    @Override
    public PageResp<JobResp> getList(JobPageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Job> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(req.getName())) {
            lambdaQueryWrapper.like(Job::getName, req.getName());
        }
        if (StrUtil.isNotEmpty(req.getAddress())) {
            lambdaQueryWrapper.eq(Job::getAddress, req.getAddress());
        }
        if (StrUtil.isNotEmpty(req.getExperience())) {
            lambdaQueryWrapper.eq(Job::getExperience, req.getExperience());
        }
        if (StrUtil.isNotEmpty(req.getEducation())) {
            lambdaQueryWrapper.eq(Job::getEducation, req.getEducation());
        }
        if (StrUtil.isNotEmpty(req.getType())) {
            lambdaQueryWrapper.eq(Job::getType, req.getType());
        }
        if (StrUtil.isNotEmpty(req.getCategory())) {
            lambdaQueryWrapper.eq(Job::getCategory, req.getCategory());
        }
        lambdaQueryWrapper.eq(Job::getExamineStatus, ExamineStatusEnum.DONE.getKey());
        lambdaQueryWrapper.eq(Job::getStatus, CommonStatusTypeEnum.NORMAL.getKey());
        Page<Job> jobPage = jobMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<JobResp>builder()
                .list(jobPage.getRecords().stream()
                        .map(item -> {
                            Company company = companyService.getById(item.getCompanyId());
                            JobResp jobResp = BeanUtil.copyProperties(item, JobResp.class);
                            jobResp.setCompanyAvatar(company.getAvatar());
                            jobResp.setCompanyName(company.getName());
                            return jobResp;
                        }).collect(Collectors.toList()))
                .total(jobPage.getTotal())
                .build();
    }

    @Override
    public PageResp<JobResp> getAdminList(JobPageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Job> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(req.getName())) {
            lambdaQueryWrapper.like(Job::getName, req.getName());
        }
        if (StrUtil.isNotEmpty(req.getAddress())) {
            lambdaQueryWrapper.eq(Job::getAddress, req.getAddress());
        }
        if (StrUtil.isNotEmpty(req.getExperience())) {
            lambdaQueryWrapper.eq(Job::getExperience, req.getExperience());
        }
        if (StrUtil.isNotEmpty(req.getEducation())) {
            lambdaQueryWrapper.eq(Job::getEducation, req.getEducation());
        }
        if (StrUtil.isNotEmpty(req.getType())) {
            lambdaQueryWrapper.eq(Job::getType, req.getType());
        }
        if (StrUtil.isNotEmpty(req.getCategory())) {
            lambdaQueryWrapper.eq(Job::getCategory, req.getCategory());
        }
        if (UserContext.getUser().getType().equals(UserTypeEnum.COMPANY.getKey())) {
            lambdaQueryWrapper.eq(Job::getCompanyId, companyService.getCompanyByUserId(UserContext.getUser().getId()).getId());
        }
        if (Objects.nonNull(req.getExamineStatus())) {
            lambdaQueryWrapper.eq(Job::getExamineStatus, req.getExamineStatus());
        }
        Page<Job> jobPage = jobMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<JobResp>builder()
                .list(jobPage.getRecords().stream()
                        .map(item -> {
                            Company company = companyService.getById(item.getCompanyId());
                            JobResp jobResp = BeanUtil.copyProperties(item, JobResp.class);
                            jobResp.setCompanyAvatar(company.getAvatar());
                            jobResp.setCompanyName(company.getName());
                            return jobResp;
                        }).collect(Collectors.toList()))
                .total(jobPage.getTotal())
                .build();
    }


    @Override
    public Job getInfo(Long jobId) {
        if (Objects.isNull(jobId)) {
            throw new ApiException("id不能为空");
        }
        return lambdaQuery()
                .eq(Job::getId, jobId)
                .eq(Job::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .one();
    }

    @Override
    public List<String> getTypeList(JobPageReq req) {
        AssertUtil.isNotEmpty(req.getCategory(), "分类不能为空");

        return new ArrayList<>(jobMapper.selectList(new LambdaQueryWrapper<Job>().eq(Job::getCategory, req.getCategory()))
                .stream()
                .map(Job::getType)
                .collect(Collectors.toSet()));
    }
}
